package com.czk.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.czk.model.hosp.HospitalSet;
import com.czk.service.HospitalSetService;
import com.czk.vo.hosp.HospitalSetQueryVo;
import com.czk.yygh.common.exception.YyghException;
import com.czk.yygh.common.result.Result;
import com.czk.yygh.common.utls.MD5;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Random;

/**
 * @Author:ChenZhangKun
 * @Date: 2021/11/3 22:17
 */
@Api(tags = "医院设置管理")
@RestController
@RequestMapping("/admin/hosp/hospitalSet")
//@CrossOrigin
public class HospitalSetController {
    @Resource
    private HospitalSetService hospitalSetService;

    @ApiOperation(value = "获取所有的医院设置信息")
    @GetMapping("/findAll")
    public Result findAll() {
        // baseService封装的
        List<HospitalSet> list = hospitalSetService.list();
        return Result.ok(list);
    }

    @ApiOperation(value = "逻辑删除医院设置信息")
    @DeleteMapping("/delete/{id}")
    public Result removeHospitalSet(@PathVariable("id") Long id) {
        return Result.ok(hospitalSetService.removeById(id));
    }

    @PostMapping("/findPage/{current}/{limit}")
    @ApiOperation(value = "分页查询")
    public Result findPageHospitalSet(@PathVariable("current") Long current,
                                      @PathVariable("limit") Long limit,
                                      @RequestBody(required = false) HospitalSetQueryVo hospitalSetQueryVo) {
        // 加入默认参数
        if (current == null) {
            current = 1L;
        }
        if (limit == null) {
            limit = 10L;
        }
        Page<HospitalSet> page = new Page<>(current, limit);
        // 构建条件
        QueryWrapper<HospitalSet> wrapper = new QueryWrapper<>();
        String hosname = hospitalSetQueryVo.getHosname();
        String hoscode = hospitalSetQueryVo.getHoscode();
        if (!StringUtils.isEmpty(hosname)) {
            wrapper.like("hosname", hosname);
        }
        if (!StringUtils.isEmpty(hoscode)) {
            wrapper.eq("hoscode", hoscode);
        }
        // 分页查询
        Page<HospitalSet> hospitalSetPage = hospitalSetService.page(page, wrapper);
        return Result.ok(hospitalSetPage);
    }

    @PostMapping("/saveHospitalSet")
    @ApiOperation(value = "添加医院")
    public Result saveHospitalSet(@RequestBody HospitalSet hospitalSet) {
        // 设置状态不可用
        hospitalSet.setStatus(1);
        // 签名秘钥
        Random random = new Random();
        hospitalSet.setSignKey(MD5.encrypt(System.currentTimeMillis() + "" + random.nextInt(100)));
        boolean save = hospitalSetService.save(hospitalSet);
        if (save) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    @ApiOperation(value = "根据id获取")
    @GetMapping("/getHospSet/{id}")
    public Result getHospSet(@PathVariable("id") Long id) {
        return Result.ok(hospitalSetService.getById(id));
    }

    @ApiOperation(value = "修改")
    @PostMapping("/updateHospitalSet")
    public Result updateHospitalSet(@RequestBody HospitalSet hospitalSet) {
        if (hospitalSetService.updateById(hospitalSet)) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    @ApiOperation(value = "批量删除")
    @DeleteMapping("/benchRemove")
    public Result benchRemove(@RequestBody List<Long> list) {
        hospitalSetService.removeByIds(list);
        return Result.ok();
    }

    @ApiOperation(value = "单个批量")
    @DeleteMapping("/remove/{id}")
    public Result remove(@PathVariable("id") long id) {
        hospitalSetService.removeById(id);
        return Result.ok();
    }

    @ApiOperation(value = "锁定或解锁医院")
    @PutMapping("/lockHospitalSet/{id}/{status}")
    public Result lockHospitalSet(@PathVariable Long id,
                                  @PathVariable Integer status) {
        // 根据id查询
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        // 设置状态
        hospitalSet.setStatus(status);
        // 更新
        hospitalSetService.updateById(hospitalSet);
        return Result.ok();
    }

    @ApiOperation(value = "发送签名秘钥")
    @PutMapping("/sendKey/{id}")
    public Result sendLockKey(@PathVariable Long id) {
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        String signKey = hospitalSet.getSignKey();
        String hoscode = hospitalSet.getHoscode();
        // TODO 发送短信
        return Result.ok();
    }

    @ApiOperation(value = "异常模拟")
    @GetMapping("/ex")
    public Result error() {
        try {
            int i = 1 / 0;
        } catch (Exception e) {
            throw new YyghException("0不能为分母", 500);
        }
        return Result.fail();
    }
}
